﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using DCT.Entities;
using DCT.Data;

public partial class Admin_HinhAnh_UploadImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UploadImage.DialogFilter = "Image Files(*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.GIF";
            BindCatalog.BindBoSuuTap(cboBoSuuTap);
        }
    }

    protected void cmdUpload_Click(object sender, EventArgs e)
    {
    }

    protected void UploadImage_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
    {
        string sAlbumThumbPath = Server.MapPath(Common.sPathUpBoSuuTap + "\\" + cboBoSuuTap.SelectedValue + "\\" + "Thumb");
        string sAlbumFullPath = Server.MapPath(Common.sPathUpBoSuuTap + "\\" + cboBoSuuTap.SelectedValue + "\\" + "Full");
        if (!Directory.Exists(sAlbumThumbPath)) Directory.CreateDirectory(sAlbumThumbPath);
        if (!Directory.Exists(sAlbumFullPath)) Directory.CreateDirectory(sAlbumFullPath);
        System.Drawing.Image originalIMG = System.Drawing.Image.FromStream(args.OpenStream());
        Bitmap imgThumb = CropSize(originalIMG, 130, 100, AnchorPosition.Center);
        Bitmap imgFull = CropSize(originalIMG, 800, 600, AnchorPosition.Center);
        imgThumb = GetBitMap(imgThumb, false);
        imgFull = GetBitMap(imgFull, true);
        imgThumb.Save(sAlbumThumbPath + "\\" + args.FileName);
        imgFull.Save(sAlbumFullPath + "\\" + args.FileName);
        imgThumb.Dispose();
        imgFull.Dispose();

        HinhAnh entHinhAnh = new HinhAnh();
        entHinhAnh.Id = 0;
        entHinhAnh.IdBoSuuTap = Convert.ToInt32(cboBoSuuTap.SelectedValue);
        entHinhAnh.DienGiai = args.FileName;
        entHinhAnh.NgayCapNhat = Common.GetServerDateTime();
        DataRepository.HinhAnhProvider.Insert(entHinhAnh);

    }


    protected void UploadImage_AttachmentRemoveClicked(object sender, CuteWebUI.AttachmentItemEventArgs args)
    {
        string sFileName = args.Item.FileName;
        string sAlbumThumbPath = Server.MapPath(Common.sPathUpBoSuuTap + "\\" + cboBoSuuTap.SelectedValue + "\\" + "Thumb");
        string sAlbumFullPath = Server.MapPath(Common.sPathUpBoSuuTap + "\\" + cboBoSuuTap.SelectedValue + "\\" + "Full");
        if (File.Exists(sAlbumThumbPath + "\\" + args.Item.FileName))
        {
            File.Delete(sAlbumThumbPath + "\\" + args.Item.FileName);
        }
        if (File.Exists(sAlbumFullPath + "\\" + args.Item.FileName))
        {
            File.Delete(sAlbumFullPath + "\\" + args.Item.FileName);
        }
        int TotalRows = 0;
        string WhereClause = string.Format("IdBoSuuTap='{0}' AND DienGiai='{1}'", cboBoSuuTap.SelectedValue, args.Item.FileName);
        TList<HinhAnh> Tlist_HinhAnh = DataRepository.HinhAnhProvider.GetPaged(WhereClause, "", 0, 0, out TotalRows);
        DataRepository.HinhAnhProvider.Delete(Tlist_HinhAnh);
    }
    protected Bitmap GetBitMap(Bitmap bitmapTemp, bool insertText)
    {
        Bitmap bmpNew = new Bitmap(bitmapTemp.Width, bitmapTemp.Height);
        Graphics oGraphic = Graphics.FromImage(bmpNew);
        oGraphic.DrawImage(bitmapTemp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bitmapTemp.Width, bitmapTemp.Height, GraphicsUnit.Pixel);
        bitmapTemp.Dispose();
        bitmapTemp = bmpNew;
        if (insertText)
        {
            SolidBrush oBrush = new SolidBrush(Color.Blue);
            Font oFont = new Font("Arial", 9, FontStyle.Bold);
            oGraphic.DrawString("DCT", oFont, oBrush, 10, 10);
            oFont.Dispose();
            oBrush.Dispose();
        }
        oGraphic.Dispose();
        return bitmapTemp;
    }

    protected Bitmap CropSize(System.Drawing.Image originalIMG, int LWidth, int LHeight, AnchorPosition Anchor)
    {
        int targetH = 0;
        int targetW = 0;
        if (originalIMG.Height > originalIMG.Width)
        {
            targetH = LHeight;
            targetW = Convert.ToInt32(originalIMG.Width * ((float)targetH / (float)originalIMG.Height));
        }
        else
        {
            targetW = LWidth;
            targetH = Convert.ToInt32(originalIMG.Height * ((float)targetW / (float)originalIMG.Width));
        }
        Bitmap bmPhoto = new Bitmap(LWidth, LHeight, PixelFormat.Format24bppRgb);
        try
        {
            bmPhoto.SetResolution(72, 72);
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            try
            {
                grPhoto.SmoothingMode = SmoothingMode.HighQuality;
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
                int newx = 0;
                int newy = 0;
                if (targetW < LWidth)
                {
                    newx = (LWidth - targetW) / 2;
                }
                if (targetH < LHeight)
                {
                    newy = (LHeight - targetH) / 2;
                }
                grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, LWidth, LHeight);
                grPhoto.DrawImage(originalIMG, new Rectangle(newx, newy, targetW, targetH), 0, 0, originalIMG.Width, originalIMG.Height, GraphicsUnit.Pixel);
            }
            catch (Exception) { }
            finally
            {
                grPhoto.Dispose();
            }
        }
        catch (Exception)
        {
        }
        return bmPhoto;
    }

    public enum AnchorPosition
    {
        Top,
        Center,
        Bottom,
        Left,
        Right
    }
}